package com.spc.car.data.config;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.spc.oa.data.mapper", sqlSessionTemplateRef="oaSqlSessionTemplate")
public class OAConfig extends AbstractDataSourceEnvironmentAware {
    @Autowired
    MybatisPlusProperties mybatisPlusProperties;

    @Override
    public void setEnvironment(Environment environment) {
        super.setEnvironment(environment);
    }

    @Bean(name = "oaSqlSessionFactory")
    public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(@Qualifier("oaDataSource") DataSource dataSource) throws Exception {
        if (null == mybatisPlusProperties) {
            mybatisPlusProperties = Binder.get(environment)
                    .bind("mybatis-plus", MybatisPlusProperties.class)
                    .orElse(new MybatisPlusProperties());
        }

        mybatisPlusProperties.setTypeAliasesPackage("com.spc.oa.data.entity");
        return super.mybatisSqlSessionFactoryBean(dataSource, mybatisPlusProperties,
                null, null);
    }

    @Bean(name = "oaDataSource",initMethod = "init", destroyMethod = "close")
    public DataSource dataSource() throws SQLException {
        DruidPoolProperties  druidPoolProperties = Binder.get(environment)
                .bind("spring.datasource.oa", DruidPoolProperties.class)
                .orElse(new DruidPoolProperties());

        return super.dataSource(druidPoolProperties);
        //return super.getAtomikosDataSourceBean(druidPoolProperties);
    }

    @Bean(name = "oaSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("oaSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "bidTransactionManager")
    public PlatformTransactionManager annotationDrivenTransactionManager(@Qualifier("oaDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "oaMapperScannerConfigurer")
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("oaSqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("com.spec.oa.mapper");
        return mapperScannerConfigurer;
    }

}
